<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
  <meta charset="utf-8" />
  <title>table模块快速使用</title>
  <link rel="stylesheet" href="/layui/css/layui.css" media="all" />
</head>
<body>
 <div class="xm-d2-hang1">
				<div class="pzright"
					style="width:100%;display: flex;justify-content: flex-start;float:right;">
					<p class="xm-d1-p2">
						<button th:if="${userRole} eq 'admin'" id="add" onclick="addRole()"
							class="layui-btn layui-btn-radius btnys">
							<i class="layui-icon">&#xe608;</i>添加角色
						</button>					
						<button class="layui-btn" id="btn-refresh">刷新</button>
					</p>
				</div>
				<div class="clear"></div>
			</div>
<table id="roleTable" lay-filter="roleTable"></table>
 
<script type="text/javascript" src="/layui/layui.all.js"></script>
<script type="text/javascript" src="/layui/layui.js"></script>

<script type="text/javascript" src="/js/jquery/jquery-3.3.1.min.js"></script>
<script type="text/javascript" th:inline="javascript">
 var uName = [[${userRole}]]
layui.use('table', function(){
  var table = layui.table;
  var form = layui.form; 
  //第一个实例
  table.render({
    elem: '#roleTable'
    ,height: 400
    ,url: '/role/findAll' //数据接口
    ,page: true //开启分页
    ,cols: [[ //表头
      {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
      ,{field: 'name', title: '角色标识', width:80, sort: true, fixed: 'left'}
      ,{field: 'notes', title: '角色注释', width:200}
     // 这个就是用来在表格上显示开关的。     
      , {templet: complain, title: '操作'}
    ]]
  });

    //监听工具条
        table.on('tool(roleTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
			if(data.permissionName==null){
				if (layEvent === 'del') {				 
							 var href = {}
	            	 		  layer.confirm('确定要删除该角色吗？', {
	        	    	        btn: ['确定','取消'] //按钮
			            	    }, function(){
	                   			  $.ajax({
					                    type:"post",
					                    url:"/menu/delMenu?id="+data.id,
					                    success:function(d){
					                        if(d=="success"){
					                     
						                 //下面是关键代码
						                 parent.layer.closeAll();
						                 layer.msg('删除成功');
						                 setTimeout(function () {location.reload()}, 330);//延迟
						 	             
       										  
					                        }
					                        else{
					                            layer.msg(d);
					                        }
					                        layer.closeAll();//关闭弹窗
					                    }
					                })
	             			 });
						
						                
		            } if (layEvent === 'edit') {
		               
	                layer.open({
			            type: 2,
			            title: '修改角色',
			            area: ['800px', '400px'], //宽高
			            fix: false, //不固定
			            maxmin: true,
			            content:'/role/editRole?rid='+data.id
			        });
	            }if (layEvent === 'editPer') {
		               
	                layer.open({
			            type: 2,
			            title: '修改权限',
			            area: ['350px', '280px'], //宽高
			            fix: true, //不固定
			            maxmin: true,
			            content:'/role/setPer?rid='+data.id
			        });
	            }
	            
			}
        });

function complain(d){//操作中显示的内容
        	if(uName=="admin"){
        		return [         	
                        '<a class="operation" lay-event="edit" href="javascript:void(0)" title="编辑">',
            	     	'<i class="layui-icon layui-icon-edit"></i></a>',
            	     	'<a class="operation" lay-event="del" href="javascript:void(0)"  title="删除">',
            	     	'<i class="layui-icon layui-icon-delete" ></i></a>',
            	     	'<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="editPer" href="javascript:void(0)"  title="设置权限">设置权限</button>',	         	     	
            	     	].join('');
        	}else{
        		return '';
        	}
        	
        }  
  
});

	function addRole() {
		layer.open({
		type: 2,
			title : '新增角色',
			area : [ '800px', '400px' ], //宽高
			fix : false, //不固定
			maxmin : true,
			content : '/role/addRole'
		});

	}
	
	   $('#btn-refresh').click(function () {
          location.reload();
        });
	
</script>

</body>
</html>